package org.micode.demo.db.syslog;

import org.micode.demo.db.dao.LogSysDao;
import org.micode.demo.db.domain.dto.LogSysDTO;
import org.micode.common.context.SpringContextHolder;
import org.micode.common.domain.base.BaseUserCache;
import org.micode.common.syslog.AbstractSysLogSaver;
import org.micode.common.utils.BeanExtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;

@Slf4j
public abstract class BaseDaoSysLogSaver extends AbstractSysLogSaver<DemoLogVO> {

    private LogSysDao logSysDao;

    public BaseDaoSysLogSaver() {
        super(DemoLogVO.class);
        logSysDao = SpringContextHolder.getBean(LogSysDao.class);
    }

    @Override
    @Async
    public void save(DemoLogVO sysLogVO) {
        sysLogVO.setOriginateModule(readAppModuleId());

        if (null != logSysDao) {
            LogSysDTO dto = new LogSysDTO();
            BeanExtUtils.copyTo(sysLogVO, dto, "operationMode");
            dto.setOperationMode(sysLogVO.getOperationMode().getCode());
            dto.setRecvMsg(sysLogVO.getRetMsg());
            dto.setUpdUser(sysLogVO.getUserId());
            dto.setUpdProg(sysLogVO.getOriginateApp());
            dto.setUpdIp(sysLogVO.getOriginateIp());

            logSysDao.insert(dto);
        } else {
            log.info("[{}]{}", sysLogVO.getOperationMode().getName(), sysLogVO);
        }
    }

    @Override
    protected void fillLogByUserCacheByLogout(DemoLogVO sysLogVO, BaseUserCache optUser) {
        // TODO: fillLogByUserCacheByLogout()
    }

    protected abstract String readAppModuleId();

}
